import axios from "axios"
import { Toast } from "vant"
const request = (config) => {
  const instance = axios.create({
    baseURL: 'http://localhost:8888/',
    timeout: 5000
  })
  // 添加请求拦截器，添加请求头里的token
  instance.interceptors.request.use(config => {
    config.headers.Authorization = JSON.parse(window.localStorage.getItem('userToken'))
    return config
  })
  instance.interceptors.response.use(res => {
    return res.data
  }, err => {
    Toast.fail(err.response.data.msg)
    // 抛出错误 阻止代码往下执行
    return Promise.reject(err)
  })
  return instance(config)
}
export {
  request
}
